/*
 * 全局权限配置
 */

 import router from "./router/router";
 import store from "./store";
 import { validatenull } from "@/utils/validate";
 import { getToken } from "@/utils/auth";
 import NProgress from "nprogress";
 import "nprogress/nprogress.css";
 import { errorToast } from "./utils/toast";

 NProgress.configure({
    showSpinner: false
 })

router.beforeEach((to, from, next) => {
  NProgress.start();
  to.meta.$keepAlive =
    to.meta.keepAlive === true && validatenull(to.meta.$keepAlive);

  const meta = to.meta || {};
  if(meta.title){
    document.title = meta.title;
    store.dispatch("setNarBarTile",meta.title);
  }

  // 设置导航头的显示与隐藏
  store.dispatch("setNarBarHidenShow",meta.isTab);

  if(getToken()) {
    to.path == "/login" ? 
      next({
        path: '/'
      }) : next();
  } else {
    meta.isAuth === false ? next() : next("/login");
  }
});

router.afterEach(() => {
  NProgress.done();
  // 检测网络连接状态
  navigator && navigator.onLine === false ?
  errorToast("网络连接不可用，请稍后再试") : "";
})
